package com.tanhua.server.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.common.mapper.AnalysisByDayMapper;
import com.tanhua.common.pojo.AnalysisByDay;
import com.tanhua.server.vo.AnalysisByDayVo;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @刘波
 * @2021/9/12 16:21
 */
@Service
public class AnalysisByDayService {

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    public PageResult queryDayByDate(Long startTime, Long endTime, String gender, Integer page, Integer pageSize) {
        long dayTime = 24 * 60 * 60 * 1000;
        int numdayTime = (int) ((endTime - startTime) / dayTime);
        List<AnalysisByDayVo> list = new ArrayList<>();
        for (int i = 0; i <=numdayTime; i++) {
            String format = new SimpleDateFormat("yyyy-MM-dd").format(startTime + (dayTime * i));
            LambdaQueryWrapper<AnalysisByDay> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AnalysisByDay::getRecordDate, format);
            AnalysisByDay record = this.analysisByDayMapper.selectOne(lambdaQueryWrapper);

            AnalysisByDayVo analysisByDayVo = new AnalysisByDayVo();
            analysisByDayVo.setRecordDate(record.getRecordDate());
            analysisByDayVo.setNumRegistered(record.getNumRegistered());
            analysisByDayVo.setNumActive(record.getNumActive());
            analysisByDayVo.setNumLogin(record.getNumLogin());
            analysisByDayVo.setNumRetention1d(record.getNumRetention1d());
            list.add(analysisByDayVo);
        }

        PageResult pageResult = new PageResult(numdayTime, pageSize, numdayTime / pageSize, page, list);
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        pageResult.setItems(list);
        return pageResult;
    }
    //重载方法
    public List<AnalysisByDayVo> queryDayByDate(Long startTime, Long endTime) {
        long dayTime = 24 * 60 * 60 * 1000;
        int numdayTime = (int) ((endTime - startTime) / dayTime);
        List<AnalysisByDayVo> list = new ArrayList<>();
        for (int i = 0; i <= numdayTime; i++) {
            String format = new SimpleDateFormat("yyyy-MM-dd").format(startTime + (dayTime * i));
            LambdaQueryWrapper<AnalysisByDay> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AnalysisByDay::getRecordDate, format);
            AnalysisByDay record = this.analysisByDayMapper.selectOne(lambdaQueryWrapper);

            AnalysisByDayVo analysisByDayVo = new AnalysisByDayVo();
            analysisByDayVo.setRecordDate(record.getRecordDate());
            analysisByDayVo.setNumRegistered(record.getNumRegistered());
            analysisByDayVo.setNumActive(record.getNumActive());
            analysisByDayVo.setNumLogin(record.getNumLogin());
            analysisByDayVo.setNumRetention1d(record.getNumRetention1d());
            list.add(analysisByDayVo);
        }
        return list;
    }
}
